package com.gtl.meishi.util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.gtl.meishi.bean.Collection;

import java.sql.SQLException;

/**
 * Created by zhangke on 16/7/2.
 * 准备数据库辅助类
 * 创建表格版本更新
 *
 */
public class DateBaseHelper extends OrmLiteSqliteOpenHelper {
private  static  final  String  DATE_BASE_NAME="collection.db";
    private static final String TAG="TAG";

    public DateBaseHelper(Context context) {
        super(context, DATE_BASE_NAME, null, 1);
    }

    @Override//表格的创建
    public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
        try {
           int state= TableUtils.createTable(connectionSource, Collection.class);
            Log.i(TAG ,"创建表格"+(state!=-1?"成功":"失败"));
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, int oldVersion, int newVersion) {
        try {
            TableUtils.dropTable(connectionSource,Collection.class,true);
            onCreate(database,connectionSource);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param context
     * @return  返回数据库辅助对象
     */
    public static  synchronized  DateBaseHelper  getDateBaseHelper(Context context){
            DateBaseHelper  helper=null;
        if(helper==null){
            helper=new DateBaseHelper(context);
            Log.i("TAG" ,"创建辅助工具类");
        }
        return helper;


    }
}
